package cn.tedu.v5;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * 192.168.166.8
 * 192.168.59.17
 * 127.0.0.1
 */
public class Client {
    private static final String HOST = "192.168.166.8";
    private static final Integer PORT = 1234;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;
    private String serverMessage;
    private String username = null;
    private String password = null;
    private String message = null;


    public Client() {
        try {
            socket = new Socket(HOST, PORT);
            ip = socket.getInetAddress().getHostAddress();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (SocketException e) {
            System.out.println("");
        } catch (UnknownHostException e) {
            System.out.println("IP地址有问题，请询问服务器端！");
        } catch (IOException e) {
            System.out.println("客户端主动退出了：断开连接");
        }
    }

    public void start() {
        try {
            System.out.println("~~~欢迎来到聊天室~~~");
            //目的：不捕获，确定没有问题，
            Scanner input = new Scanner(System.in);
            //接收登录提示
            //1.读入服务器--输出流
            //服务器端：写“请输入用户名：”
            serverMessage = br.readLine();
            System.out.println(serverMessage);
            username = input.nextLine();
            pw.println(username);

            serverMessage = br.readLine();
            System.out.println(serverMessage);
            password = input.nextLine();
            pw.println(password);

            serverMessage = br.readLine();
            System.out.println(serverMessage);
            //3.2判断如果是“登录成功欢迎进入聊天室”  前缀：登录成功
            if (serverMessage.startsWith("登录成功")) {
                //true  聊天
                chat(input);
            }
            if (serverMessage.equalsIgnoreCase("exit")){
                System.exit(0);
            }
        } catch (IOException e) {
            System.out.println("客户端断开链接");
        } finally {
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器的链接失败了~~~");
                }
            if (pw != null)
                pw.close();
            if (socket != null && !socket.isClosed()) {
                System.out.println("客户端关闭成功！");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            }
        }
    }

    public static void main(String[] args) {
        new Client().start();
    }

    private void chat(Scanner input) {
        new Thread(() -> {
            try {
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println(message);
                    System.out.println("客户端请输入：");
                }
            } catch (IOException e) {
                System.out.println("服务器端发来的信息断开了");
            }
        }).start();
        System.out.println("请客户端输入：");
        Scanner scanner= new Scanner(System.in);
        while (true) {
            String say = scanner.nextLine();
            pw.println("客户端说：" + say);
            if (say.equalsIgnoreCase("exit")){
                System.exit(0);
            }
        }
    }
}
